package com.moodincode.taskcenter.script;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author moodincode
 */
public class ScriptInjectUtil {
    private static Logger log= LoggerFactory.getLogger(ScriptInjectUtil.class);
    public static final String INJECT_CLASS_SCRIPT="" +
            "var JsonUtil=Java.type(\"com.moodincode.taskcenter.script.JsonUtil\");" //JSON工具类
                    +"var $=Java.type(\"com.moodincode.taskcenter.script.AjaxUtil\");";//ajax请求工具类,$.ajax
    /**
     * 注入基本对象
     * @param nashornEngine
     */
    public static void injectJavaClass(ScriptEngine nashornEngine){
        if(nashornEngine==null){
            return;
        }
        try {
            nashornEngine.eval(INJECT_CLASS_SCRIPT);
        } catch (ScriptException e) {
          log.error("注入Java类失败,原因:",e);
        }
    }


    /**
     * 获取脚本引擎
     * @return
     */
    public static ScriptEngine getScriptEngine(){
        ScriptEngineManager factory = new ScriptEngineManager();
        return factory.getEngineByName("nashorn");
    }


}
